﻿Imports System.Runtime.InteropServices

Public Class EdgeBarManager
    Private _edgeBarPages As Dictionary(Of IntPtr, EdgeBarPage)

    Public Sub New()
        _edgeBarPages = New Dictionary(Of IntPtr, EdgeBarPage)
    End Sub

    Public Sub AddPage(addin As SolidEdgeFramework.AddIn, document As SolidEdgeFramework.SolidEdgeDocument)
        Dim pUnk As IntPtr
        Dim edgeBar As SolidEdgeFramework.ISolidEdgeBarEx
        Dim edgeBarPage As EdgeBarPage
        Dim edgeBarControl As EdgeBarControl

        pUnk = Marshal.GetIUnknownForObject(document)

        If Not _edgeBarPages.ContainsKey(pUnk) Then
            edgeBar = addin
            edgeBarControl = New EdgeBarControl()
            edgeBarControl.Document = document
            edgeBarPage = New EdgeBarPage(edgeBar, document, edgeBarControl, 103)
            _edgeBarPages.Add(pUnk, edgeBarPage)
        End If
    End Sub

    Public Sub RemovePage(document As SolidEdgeFramework.SolidEdgeDocument)
        Dim pUnk As IntPtr
        Dim edgeBarPage As EdgeBarPage

        pUnk = Marshal.GetIUnknownForObject(document)
        If _edgeBarPages.ContainsKey(pUnk) Then
            edgeBarPage = _edgeBarPages(pUnk)
            _edgeBarPages.Remove(pUnk)
            edgeBarPage.Dispose()
        End If
    End Sub

    Public Sub RemoveAllPages()
        Dim edgeBarPages() As EdgeBarPage
        Dim edgeBarPage As EdgeBarPage

        edgeBarPages = _edgeBarPages.Values.ToArray()

        For Each edgeBarPage In edgeBarPages
            edgeBarPage.Dispose()
        Next

        _edgeBarPages.Clear()
    End Sub
End Class
